<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select v-model="query.schoolId" style="width: 185px;" class="filter-item" placeholder="学校名称" clearable>
          <el-option v-for="school in schoolArray" :key="school.name" :label="school.name" :value="school.id" />
        </el-select>
        <el-input v-model="query.name" clearable placeholder="年级名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="关联学校" prop="school['id']">
            <el-select v-model="form.school.id" placeholder="选择学校" clearable style="width: 370px;">
              <el-option v-for="school in schoolArray" :key="school.name" :label="school.name" :value="school.id" />
            </el-select>
            <!--<el-autocomplete
              v-model="form.school.name"
              :fetch-suggestions="queryAddDetail"
              placeholder="选择学校"
              style="width: 370px"
              property="search"
              clearable
              @select="handleSelect"
            />-->
          </el-form-item>
          <el-form-item label="年级名称" prop="name">
            <el-input v-model="form.name" placeholder="输入年级" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="school.name" label="关联学校" />
        <el-table-column prop="name" label="年级名称" />
        <el-table-column prop="gmtCreate" label="创建时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtCreate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="gmtModified" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.gmtModified) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','grade:edit','grade:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudGrade from '@/api/pay/grade'
import crudSchool from '@/api/pay/school'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, school: { id: null, name: null }, name: null, gmtCreate: null, gmtModified: null }
export default {
  name: 'Grade',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '年级', url: 'api/grade', sort: 'id,desc', crudMethod: { ...crudGrade }})
  },
  data() {
    const validSchool = (rule, value, callback) => {
      if (value.id === '' || value.id === null) {
        callback(new Error('请选择学校'))
      }
    }
    return {
      permission: {
        add: ['admin', 'grade:add'],
        edit: ['admin', 'grade:edit'],
        del: ['admin', 'grade:del']
      },
      rules: {
        school: [
          { required: true, validator: validSchool, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入年级名称', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'name', display_name: '年级名称' },
        { key: 'schoolId', display_name: '学校名称' }
      ],
      schoolName: null,
      schoolArray: []
    }
  },
  mounted() {
    // 获取所有学校
    crudSchool.query().then(res => {
      this.schoolArray = res.content
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    // input动态查询
    queryAddDetail(queryString, cb) {
      this.schoolArray.forEach(item => {
        item.value = item.name
      })
      const results = queryString ? this.schoolArray.filter(this.createFilter(queryString)) : this.schoolArray
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (state) => {
        return (state.value.indexOf(queryString) === 0)
      }
    },
    handleSelect(item) {
      this.form.school.id = item.id
    },
    querySelect(item) {
      this.crud.query.schoolId = item.id
    }
  }
}
</script>

<style scoped>

</style>
